package com.hdax.gulimall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdax.common.utils.Query;
import com.hdax.common.utils.R;
import com.hdax.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hdax.gulimall.product.entity.PmsBrandEntity;
import com.hdax.gulimall.product.service.CategoryBrandRelationService;
import com.hdax.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {



    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取某个分类下关联所有品牌的信息
     * @param catId
     * @return
     */
    //    ///product/categorybrandrelation/brands/list
    @GetMapping("/brands/list")
    public R  relationBrandList(@RequestParam("catId") Long catId){
        List<PmsBrandEntity> list=categoryBrandRelationService.getBrandsByCateId(catId);
        List<BrandVo> collect = list.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(item, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }
    /**
     * 查询品牌关联的分类
     * @param brandId
     * @return
     */

    @RequestMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId")Long brandId){
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );
        return R.ok().put("data",list);
    }


    /**
     * 保存品牌和分类和品牌分类的名称
     */

    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelationEntity){
        boolean save = categoryBrandRelationService.saveDetail(categoryBrandRelationEntity);
        return R.ok();
    }



}
